package com.example.userservice.controller;

import com.example.common.dto.*;
import com.example.userservice.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
public class UserController {
    
    private final UserService userService;
    
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/register")
    public ApiResponse<String> register(@RequestBody UserDTO userDTO, HttpServletRequest request) {
        String clientIp = request.getRemoteAddr();
        return userService.register(userDTO, clientIp);
    }

    @PostMapping("/login")
    public ApiResponse<String> login(@RequestParam String username, @RequestParam String password) {
        return userService.login(username, password);
    }

    @GetMapping("/{userId}")
    public ApiResponse<UserDTO> getUserInfo(@PathVariable Long userId) {
        return userService.getUserInfo(userId);
    }

    @PutMapping("/{userId}")
    public ApiResponse<Void> updateUser(@PathVariable Long userId, @RequestBody UserDTO userDTO) {
        return userService.updateUser(userId, userDTO);
    }

    @PostMapping("/reset-password")
    public ApiResponse<Void> resetPassword(@RequestParam Long userId, @RequestParam String newPassword) {
        return userService.resetPassword(userId, newPassword);
    }
}